using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;


namespace AspAlliance.SimpleCms
{
    public class Content_Management : IHttpHandler
    {
        public void ProcessRequest(HttpContext Context)
        {
            CmsSettings CmsConfig = (CmsSettings)ConfigurationManager.GetSection("CMSConfig");

            string RequestedPage = Context.Request.AppRelativeCurrentExecutionFilePath.ToString();
            if (RequestedPage == CmsConfig.AdminPage)
            {
                Context.Response.Redirect("~/admin.aspx");
            }
            if (RequestedPage == CmsConfig.EditPage)
            {
                Context.Response.Redirect("~/edit.aspx");
            }


            DataTable pageDataTable = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings[CmsConfig.ConnectionString].ConnectionString, CommandType.Text, "SELECT * FROM [cmsPageFile] WHERE [PageName] = @RequestedPage", new SqlParameter[] { new SqlParameter("@RequestedPage", RequestedPage) }).Tables[0];

            if (pageDataTable.Rows.Count != 1)
            {
                throw new HttpException(404, "File Not Found");
            }
            else
            {
                DataRow RequestedPageData = pageDataTable.Rows[0];

                //return new AdminPage();

                //Context.Response.Write("<html>\n\t<head>\n\t\t<title>");
                //Context.Response.Write(RequestedPageData["Title"]);
                //Context.Response.Write("</title>\n\t</head>\n");
                //Context.Response.Write("\t<body>\n");
                //Context.Response.Write(RequestedPageData["PageContent"]);
                //Context.Response.Write("\n\t</body>\n</html>");

            }
            
        }

        #region IHttpHandler Members

        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }

        #endregion

    }
}
